import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_cpp_sdk/flutter_cpp_sdk.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();

    /// NOTE wujehy 这一步很重要 , 初始化 全局只要一次即可
    ClientApp.initNativeChannel();
    // initPlatformState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: SingleChildScrollView(
          child : new Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Column(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.center,
              ),
              RaisedButton(
                child: Text('初始化客户端', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  ClientApp.init().then((value) {
                    print("value : ${value}");
                  });
                },
              ),
              RaisedButton(
                child: Text('反初始化客户端', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  ClientApp.deinit().then((value) {
                    print("value : ${value}");
                  });
                },
              ),
              RaisedButton(
                child: Text('获取客户端状态', style: TextStyle(fontSize: 20)),
                onPressed: () {},
              ),
              RaisedButton(
                child: Text('设置一个缓存', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  ClientApp.setBaseKey("test1", "testvalue").then((value){
                    print("set key callback ${value.code} , ${value.errmsg}");
                  });
                },
              ),
              RaisedButton(
                child: Text('删除一个缓存', style: TextStyle(fontSize: 20)),
                onPressed: () {
                    ClientApp.delBaseKey("test1").then((value){
                    print("del key callback ${value.code} , ${value.errmsg}");
                  });
                },
              ),
              RaisedButton(
                child: Text('获取一个缓存', style: TextStyle(fontSize: 20)),
                onPressed: () {
                     ClientApp.getBaseKey("test1").then((value){
                    print("get key callback ${value.code} , ${value.errmsg} , data = ${value.dataStr}");
                  });
                },
              ),
              RaisedButton(
                child: Text('多线程获取共享缓存', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  //TODO 暂时没时间写
                },
              ),
              RaisedButton(
                child: Text('测试阻塞接口', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  // NOTE wujehy 这个是同步接口, 点击按钮后 , 日志在处理, 按钮不能动
                  TestApp.Sync().then((value){
                    print(" TestApp.Sync callback : ${value.code} ${value.errmsg} , data : ${value.dataStr}");
                  });
                },
              ),
              RaisedButton(
                child: Text('测试异步接口', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  // NOTE wujehy : 这个是异步接口, 点击按钮后, 任务还没结束, 但是列表可以滑动 , 不卡
                    TestApp.Async().then((value){
                    print(" TestApp.Sync callback : ${value.code} ${value.errmsg} , data : ${value.dataStr}");
                  });
                },
              ),
              RaisedButton(
                child: Text('测试异步回包接口', style: TextStyle(fontSize: 20)),
                onPressed: () {
                  RequestDataType requestDataType = new RequestDataType();
                  requestDataType.taskid = 1 ;
                  requestDataType.status = true;
                  requestDataType.token = "testToken";
                  requestDataType.type = 2;
                  List<int> testList = [96,97 ,98, 99];
                  requestDataType.data = Uint8List.fromList(testList);

                TestApp.CustomizeRequest(requestDataType).then((value){
                  print("CustomizeRequest callback :  ${value.code} ${value.errmsg} , ${value.response.taskid}");
                });

                },
              ),
            ]),
      ),
    ));
  }
}
